//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  AddCircleBoard.m
//  iHeima
//
//  Created by he chao on 6/5/14.
//  Copyright (c) 2014 leon. All rights reserved.
//

#import "AddCircleBoard.h"

#pragma mark -

@interface AddCircleBoard()
{
	//<#@private var#>
}
@end

@implementation AddCircleBoard
DEF_SIGNAL(COMMIT)
DEF_SIGNAL(PHOTO)
DEF_SIGNAL(CAMERA)
DEF_SIGNAL(LIBRARY)

- (void)load
{
    arrayImages = [[NSMutableArray alloc] init];
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"发布消息";
    [self showNaviBar];
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    //self.view.backgroundColor = RGB(246, 246, 246);
    [self loadContent];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.stack popBoardAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

ON_SIGNAL2(BeeUITextView, signal) {
    if ([signal is:BeeUITextView.RETURN]) {
        [textView resignFirstResponder];
    }
}

ON_SIGNAL2(AddCircleBoard, signal){
    if ([signal is:AddCircleBoard.PHOTO]) {
        BeeUIActionSheet *sheet = [BeeUIActionSheet spawn];
        sheet.title = @"请选择操作";
        [sheet addButtonTitle:@"拍照" signal:AddCircleBoard.CAMERA];
        [sheet addButtonTitle:@"从相册选择" signal:AddCircleBoard.LIBRARY];
        [sheet addCancelTitle:@"取消"];
        [sheet showInViewController:self];
    }
    else if ([signal is:AddCircleBoard.COMMIT]){
        if (textView.text.length == 0) {
            [[BeeUITipsCenter sharedInstance] presentMessageTips:@"请输入消息"];
            return;
        }
        if (arrayImages.count==0) {
            [[BeeUITipsCenter sharedInstance] presentMessageTips:@"请选择照片"];
            return;
        }
        BeeHTTPRequest *request = [self POST:[NSString stringWithFormat:@"%@/apifeeds/quansend",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"content",textView.text).PARAM(@"session_id",kUserInfo[@"session_id"]);//.FILE(@"img0",UIImageJPEGRepresentation(image, 0.4));
        for (int i = 0; i < arrayImages.count; i++) {
            request.FILE([NSString stringWithFormat:@"img%d",i],UIImageJPEGRepresentation(arrayImages[i], 0.4));
        }
        [[BeeUITipsCenter sharedInstance] presentLoadingTips:@"正在发送"];
    }
    else if ([signal is:AddCircleBoard.CAMERA]) {
        UIImagePickerController * c = [[UIImagePickerController alloc] init];
        c.delegate = self;
        c.sourceType = UIImagePickerControllerSourceTypeCamera;
        //c.allowsEditing = YES;
        [self presentViewController:c animated:YES completion:nil];
        
    }
    else if ([signal is:AddCircleBoard.LIBRARY]) {
        UIImagePickerController * c = [[UIImagePickerController alloc] init];
        c.delegate = self;
        c.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //c.allowsEditing = YES;
        [self presentViewController:c animated:YES completion:nil];
    }
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        id json = [request.responseString mutableObjectFromJSONString];
        
        switch ([json[@"status"] intValue]) {
            case 1:
                
            {
                
                NSLog(@"------返回:%@",json);
                
                [[BeeUITipsCenter sharedInstance] presentMessageTips:@"发送成功"];
                
                [[NSNotificationCenter defaultCenter] postNotification:@"myNoti" withObject:Nil];
                
                [self.stack popBoardAnimated:YES];
            }
                break;
        }
    }
}

- (void)loadContent{
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.viewWidth, 260)];
    vi.backgroundColor = RGB(244, 244, 244);
    [self.view addSubview:vi];
    
    textView = [BeeUITextView spawn];
    textView.frame = CGRectMake(20, 15, 290, 140);
    textView.layer.borderWidth = 0.5;
    textView.backgroundColor = [UIColor whiteColor];
    textView.layer.borderColor = RGB(174, 174, 174).CGColor;
    textView.returnKeyType = UIReturnKeyDone;
    textView.font = FONT(15);
    [vi addSubview:textView];
    
    [self loadImags];
    
    BaseButton *btn = [BaseButton initBaseBtn:IMAGESTRING(@"btn1") highlight:IMAGESTRING(@"btn1_pre") text:@"提交" textColor:[UIColor whiteColor] font:FONT(14)];
    btn.frame = CGRectMake(65, vi.bottom+20, 190, 44);
    [btn addSignal:AddCircleBoard.COMMIT forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [textView resignFirstResponder];
}

- (void)loadImags{
    for (UIView *element in self.view.subviews) {
        if (element.tag == 9527) {
            [element removeFromSuperview];
        }
    }
    for (int i = 0; i < arrayImages.count; i++) {
        BeeUIImageView *imgV = [BeeUIImageView spawn];
        imgV.frame = CGRectMake(20+95*i, textView.bottom+12, 78, 78);
        imgV.contentMode = UIViewContentModeScaleAspectFit;
        imgV.image = arrayImages[i];
        imgV.tag = 9527;
        [self.view addSubview:imgV];
    }
    
    if (arrayImages.count<3) {
        BeeUIButton *btn = [BeeUIButton spawn];
        btn.tag = 9527;
        [btn setImage:IMAGESTRING(@"add_pic") forState:UIControlStateNormal];
        btn.frame = CGRectMake(20+arrayImages.count*95, textView.bottom+12, 78, 78);
        [btn addSignal:AddCircleBoard.PHOTO forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}

#pragma mark - QBImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(id)info
{
    
    UIImage * i = [info[@"UIImagePickerControllerOriginalImage"] copy];
    if(i.size.width > 640)
    {
        i = [self scaleImage:i toScale:640/i.size.width];
    }
    
    [self dismissViewControllerAnimated:YES completion:NULL];
    //[self sendImage:i];
    [arrayImages addObject:i];
    [self loadImags];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePickerController
{
    //NSLog(@"Cancelled");
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}


@end
